'use strict';

let nunjucks = require('nunjucks');

function creatEnv(path, opts) {
    path || 'views';
    opts || {};
    let confOtps = {
        autoescape: opts.autoescape || true,
        throwOnUndefined: opts.throwOnUndefined || false,
        trimBlocks: opts.trimBlocks || true,
        lstripBlocks: opts.lstripBlocks || true,
        watch: opts.watch || true,
        noCache: opts.noCache || true
    }

    let env = nunjucks.configure(path, confOtps);

    return env;
}

module.exports = async (ctx, next) => {
    ctx.render = function (view, model) {
        let env = creatEnv();
        ctx.body = env.render(view, model);
    }
    await next();
}